#include "StdAfx.h"
#include "GoogleUserData.h"
#include "Shlobj.h"

namespace GoogleProtocol
{

CGoogleUserData::CGoogleUserData(void)
{
}

CGoogleUserData::~CGoogleUserData(void)
{
}

BOOL CGoogleUserData::LoadUserData(void)
{
	CHAR szProfilePath[_MAX_PATH];
	BOOL bResult = FALSE;

	bResult = GetProfilePath(szProfilePath);
	CHECK_RESULT(bResult);
	strcat(szProfilePath, "\\user.dat");

	//// open ueser profile files.
	//CHAR buffer[4096];
	//int nRead = 0;
	//errno_t err;
	//FILE* stream;

	//err = _tfopen_s(&stream, szProfilePath, _T("r"));
	//CHECK_RESULT(err == 0);

	//memset(buffer, 0, 4096);
	//nRead = fread(buffer, sizeof(CHAR), 4095, stream);
	//if (nRead == 0)
	//{
	//	fclose(stream);
	//}

	// parse username, auth
	//
	// [GoogleAccount]
	// User="username"
	// Auth="1234567890"

	CHAR szUser[256];
	CHAR szPwd[256];
//	CHAR szAuth[1024];

	GetPrivateProfileStringA("GoogleAccount", "User", NULL, szUser, 256, szProfilePath);
	GetPrivateProfileStringA("GoogleAccount", "Pwd", NULL, szPwd, 256, szProfilePath);
//	GetPrivateProfileStringA("GoogleAccount", "Auth", NULL, szAuth, 256, szProfilePath);

	m_strUserName = szUser;
	m_strUserPwd = szPwd;
//	m_GoogleAuth.m_strAuth = szAuth;
	bResult = TRUE;

RESULT:
	return bResult;
}

int CGoogleUserData::SaveUserData(void)
{
	CHAR szProfilePath[_MAX_PATH];
	BOOL bResult = FALSE;

	bResult = GetProfilePath(szProfilePath);
	CHECK_RESULT(bResult);

	// verify directory exist
	WIN32_FIND_DATAA findData;
	HANDLE hFindFile;

	hFindFile = FindFirstFileA(szProfilePath, &findData);
	if (hFindFile == INVALID_HANDLE_VALUE)
	{
		int Ret = SHCreateDirectoryExA(NULL, szProfilePath, NULL);
		if (Ret != ERROR_SUCCESS)
		{
			FindClose(hFindFile);
			return -1;
		}
	}

	strcat(szProfilePath, "\\user.dat");
	FindClose(hFindFile);

	//// open ueser profile files.
	//CHAR buffer[4096];
	//int nRead = 0;
	//errno_t err;
	//FILE* stream;

	//err = _tfopen_s(&stream, szProfilePath, _T("r"));
	//CHECK_RESULT(err == 0);

	//memset(buffer, 0, 4096);
	//nRead = fread(buffer, sizeof(CHAR), 4095, stream);
	//if (nRead == 0)
	//{
	//	fclose(stream);
	//}

	// parse username, auth
	//
	// [GoogleAccount]
	// User="username"
	// Auth="1234567890"

	WritePrivateProfileStringA("GoogleAccount", "User", m_strUserName.c_str(), szProfilePath);
	WritePrivateProfileStringA("GoogleAccount", "Pwd", m_strUserPwd.c_str(), szProfilePath);
//	WritePrivateProfileStringA("GoogleAccount", "Auth", m_GoogleAuth.m_strAuth.c_str(), szProfilePath);
	bResult = TRUE;

RESULT:
	return bResult;
}

LPCSTR CGoogleUserData::GetUserName(void)
{
	if (m_strUserName.empty())
		return "";

	return m_strUserName.c_str();
}

LPCSTR CGoogleUserData::GetUserPwd(void)
{
	if (m_strUserPwd.empty())
		return "";

	return m_strUserPwd.c_str();
}

CGoogleAuth* CGoogleUserData::GetAuthToken(void)
{
	return &m_GoogleAuth;
}

LPCWSTR CGoogleUserData::GetUniqueString(void)
{
	if (m_strUniqueString.empty())
		return L"";

	return m_strUniqueString.c_str();
}

void CGoogleUserData::SetAuthToken(const CGoogleAuth* pNewAuth)
{
	_ASSERT(pNewAuth != NULL);
	m_GoogleAuth = *pNewAuth;
}
void CGoogleUserData::SetUserName(LPCSTR lpUserName)
{
	m_strUserName = lpUserName;
}

void CGoogleUserData::SetUserPwd(LPCSTR lpUserPwd)
{
	m_strUserPwd = lpUserPwd;
}

BOOL CGoogleUserData::GetProfilePath(LPSTR lpProfilePath)
{
	LPITEMIDLIST pidlItem = NULL;         // The item's PIDL.
	HRESULT hres;
	BOOL bResult = FALSE;

	hres = SHGetSpecialFolderLocation(NULL,
		CSIDL_APPDATA,
		&pidlItem);
	CHECK_RESULT(BOOL(SUCCEEDED(hres)));
	CHECK_RESULT(SHGetPathFromIDListA(pidlItem, lpProfilePath));

	LPCSTR lpUserData = "\\DocoMedia\\HomeMediaBrowser";
	strcat_s(lpProfilePath, _MAX_PATH - 1, lpUserData);
	bResult = TRUE;

RESULT:
	return bResult;
}

}
